<template>
  <el-card>
    <div>
      <el-table
        center
        border
        :data="
          tableData.filter(
            (data) =>
              !search || data.name.toLowerCase().includes(search.toLowerCase())
          )
        "
        fit
        highlight-current-row
        current-row-key
        stripe
        style="width: 100%"
        :row-style="{ height: '80px' }"
      >
        <el-table-column label="姓名" prop="name" align="center">
        </el-table-column>
        <el-table-column
          label="性别"
          prop="sex"
          align="center"
        ></el-table-column>
        <el-table-column label="头像" align="center" prop="avatar"
          ><template scope="scope">
            <el-image
              :src="scope.row.avatar"
              style="width: 80px; height: auto"
              title="用户头像"
              alt="头像加载失败"
            /> </template
        ></el-table-column>
        <el-table-column
          label="注册时间"
          prop="gmtCreate"
          align="center"
        ></el-table-column>
        <el-table-column
          label="学历"
          prop="degree"
          align="center"
        ></el-table-column>
        <el-table-column label="状态" prop="status" align="center">
          <template scope="scope">
            <span v-if="scope.row.status === 0">离线</span>
            <span v-if="scope.row.status === 1">在线</span>
          </template>
        </el-table-column>
        <el-table-column align="right">
          <template slot="header" slot-scope="scope" align="center">
            <el-input
              v-model="search"
              size="mini"
              placeholder="输入关键字搜索"
            />
          </template>
          <template slot-scope="scope">
            <el-button
              class="editButton"
              size="mini"
              @click="getCode(scope.row)"
              >获取信息</el-button
            >
            <el-button
              class="browserButton"
              style="margin-right:10px"
              size="mini"
              @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-dialog
              title="修改用户信息"
              :visible.sync="dialogFormVisible"
              width="40%"
              center
            >
              <el-form :model="editForm" ref="editForm" label-width="80px">
                <el-form-item label="用户姓名">
                  <el-input v-model="editForm.name"></el-input>
                </el-form-item>
                <el-form-item label="性别">
                  <el-radio-group v-model="editForm.sex">
                    <el-radio label="男"></el-radio>
                    <el-radio label="女"></el-radio>
                  </el-radio-group>
                </el-form-item>
                <el-form-item label="文化水平">
                  <el-select v-model="editForm.degree" placeholder="选择学历">
                    <el-option label="小学" value="小学"></el-option>
                    <el-option label="中学" value="初中"></el-option>
                    <el-option label="高中" value="高中"></el-option>
                    <el-option label="大学本科" value="大学本科"></el-option>
                  </el-select>
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button @click="cancel('editForm')">取 消</el-button>
                <el-button type="primary" @click="submitForm">确 定</el-button>
              </div>
            </el-dialog>
            <el-button
              size="mini"
              class="deleteButton"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          prev-text="上一页"
          next-text="下一页"
          center
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>

    <el-dialog
      title="用户二维码"
      :visible.sync="CodeDialogVisible"
      width="15%"
    >
      <img style="margin:5px auto;margin-left:2vw" :src="imgUrl" alt="" />
      <span slot="footer" class="dialog-footer">
        <el-button @click="CodeDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="CodeDialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
import {
  getUsers,
  deleteUserById,
  getUserById,
  updateUser,
  getCode,
} from "@/api/user";
export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      search: "",
      total: 0,
      size: 10,
      editForm: {
        id: null,
        name: "",
        sex: "",
        degree: "",
      },
      dialogFormVisible: false,
      CodeDialogVisible: false,
      imgUrl: "",
    };
  },
  created() {
    getUsers(1, 10).then((res) => {
      this.tableData = res.data.list;
      this.total = res.data.total;
    });
  },
  methods: {
    getCode(row) {
        this.imgUrl = 'http://47.98.246.42:8080/manager/user/getUserQRCode/' + row.id
        this.CodeDialogVisible = true

    },
    pageUsers(current, limit) {
      getUsers(current, limit).then((res) => {
        this.tableData = res.data.list;
        this.total = res.data.total;
      });
    },
    handleSizeChange(val) {
      this.size = val;
      this.pageUsers(this.currentPage, val);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.pageUsers(val, this.size);
    },
    handleEdit(index, row) {
      //   console.log(index, row);
      this.dialogFormVisible = true;
      getUserById(row.id).then((res) => {
        this.editForm = res.data.user;
        this.editForm.id = row.id;
      });
    },
    cancel(editForm) {
      this.dialogFormVisible = false;
      this.editForm = this.$options.data().editForm;
    },
    submitForm() {
      this.$delete(this.editForm, "gmtCreate");
      this.$delete(this.editForm, "gmtModified");
      updateUser(this.editForm).then((res) => {
        this.$message.success("修改成功");
        this.pageUsers(this.currentPage, this.size);
        this.dialogFormVisible = false;
      });
    },
    handleDelete(index, row) {
      console.log(index, row);
      deleteUserById(row.id).then((res) => {
        this.$message.success("删除成功");
        this.pageUsers(this.currentPage, this.size);
      });
    },
  },
};
</script>
<style  scoped>
.block {
  margin-top: 2%;
  display: flex;
  flex-direction: row;
  justify-content: center;
}
</style>